#!/usr/bin/env python
#
#   Copyright (C) 2006 Jonathan Saggau                                     
#   saggau@gmail.com                                                       
#                                                                          
#   This program is free software; you can redistribute it and/or modify  
#   it under the terms of the GNU General Public License as published by  
#   the Free Software Foundation; either version 2 of the License, or     
#   (at your option) any later version.                                    
#                                                                          
#   This program is distributed in the hope that it will be useful,        
#   but WITHOUT ANY WARRANTY; without even the implied warranty of        
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         
#   GNU General Public License for more details.                          
#                                                                          
#   You should have received a copy of the GNU General Public License     
#   along with this program; if not, write to the                         
#   Free Software Foundation, Inc.,                                        
#   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.  
#

from supyrconductor import node, synth
from model import Model
import deftxt

class Loader(Model):
    """Generates the proper synthdef name and tells the server to load it 
    up for the trigger object.
    
    See generator.py for the tool that makes the batch of synthdefs needed.
    
    When changing certain attributes of the trigger object, we have to 
    reinstantiate the synth on the server.  This handles that activity."""
    
    _loaded = False
    def __init__(self, controller, nodeID, bufferID):
        Model.__init__(self, nodeID, bufferID)
        self.controller = controller
    
    def _unloadSynth(self):
        if self._loaded:
            self.controller.listSendMsg(node.free(self.nodeID))
            self._loaded = False

    def _loadSynth(self, action=synth.HEAD, target=0):
        bundle = []
        if self._loaded:
            bundle.append(node.free(self.nodeID))
        bundle.append(
                      synth.new(self.synthName(), self.nodeID,
                                action, target, self._synthargs())
                     )
        self.controller.sendBundleAbs(-1, bundle) # -1 means execute it now.
        incoming = self.controller.receive('/tr', '/n_go', '/fail')
        # sometimes we get a trigger in there before we get the /n_go
        # should probably make sure the trigger came from the right place...
        # then again, this .receive is here to delay the next n_set that the
        # gui does by itself to set the input.  Seems to work so far
        self._loaded = True

#These overrides are the only attributes that need a synth re-load
    def setOverlap(self, overlap):
        Model.setOverlap(self, overlap)
        self._loadSynth()

    def setEnvType(self, envType):
        Model.setEnvType(self, envType)
        self._loadSynth()

    def setChannels(self, channels):
        Model.setChannels(self, channels)
        self._loadSynth()

    def setSynthType(self, type):
        Model.setSynthType(self, type)
        self._loadSynth()
        
    def setNodeID(self, id):
        Model.setNodeID(self, id)
        self._loadSynth()